package cn.edu.scau.cmi.maikejian.hibernate;

import java.sql.Connection;
import java.util.Scanner;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.edu.scau.cmi.maikejian.comprehension.dao.JdbcConnectionUtil;
import cn.edu.scau.cmi.maikejian.comprehension.dao.StudentDAO;
import cn.edu.scau.cmi.maikejian.comprehension.dao.TeacherDAO;
import cn.edu.scau.cmi.maikejian.comprehension.domain.Student;
import cn.edu.scau.cmi.maikejian.comprehension.domain.Teacher;
import cn.edu.scau.cmi.maikejian.comprehension.util.ComprehensionApplicationContextUtil;

public class HibernateClientJdbc {
	
	private Scanner input = new Scanner(System.in);
	private Connection conn;
	StudentDAO stu_dao ;
	TeacherDAO tea_dao;
	public static void main(String[] args) {
		HibernateClientJdbc a = new HibernateClientJdbc();
		a.UI();
	}
	
	public HibernateClientJdbc() {
		//this.conn = (Connection) JdbcConnectionUtil.getConnection();
		//stu_dao = new StudentDAO(conn);
		//tea_dao = new TeacherDAO(conn);
		ApplicationContext ac = new ClassPathXmlApplicationContext("cn/edu/scau/cmi/maikejian/comprehension/util/comprehensionApplicationContext.xml");
		stu_dao =(StudentDAO) ac.getBean("studentDAO");
		tea_dao =(TeacherDAO) ac.getBean("teacherDAO");
		
	}
	
	public void UI() {
		
		int select;
		while(true) {
			System.out.println("输入序号以选择功能");
			System.out.println("功能1：添加学生\t\t" + 
					"功能2：修改学生\r\n" + 
					"功能3：删除学生\t\t" +
					"功能4：查询学生\r\n" +
					"功能5：添加教师\t\t" + 
					"功能6：修改教师\r\n" + 
					"功能7：删除教师\t\t" + 
					"功能8：查询老师指导的学生\r\n" +
					"功能9：学生选择指导教师\t\t" + 
					"功能10：学生修改指导教师\r\n" + 
					"功能11：教师选择被指导的学生\t" + 
					"功能12：教师修改被指导的学生\r\n"+
					"功能0：退出\n");
			select = input.nextInt();
			switch (select){
				case 1:newStudnet();break;
				case 2:updateStudent();break;
				case 3:deleteStudnet();break;
				case 4:getStudent();break;
				case 5:newTeacher();break;
				case 6:updateTeacher();break;
				case 7:deleteTeacher();break;
				case 8:getInstructor();break;
				case 9:selectTutor();break;
				case 10:updateTutor();break;
				case 11:selectStudent();break;
				case 12:updateINTRStudent();break;
				case 0:return;
			}
			
			
		}
		
		
	}
	
	public void newStudnet() {
		System.out.println("输入要增加学生的信息：");
		System.out.print("输入学生的学号：");
		long id = input.nextLong();
		System.out.print("输入学生的姓名");
		String name = input.next();
		Student student = new Student(id,name);
		StudentDAO.newStudent(student);
		System.out.println("增加成功");
		StudentDAO.listAllstudents();
	}
	
	public void deleteStudnet() {
		System.out.println("请输入要删除的学生的id：");
		long id = input.nextLong();
		Student stu = StudentDAO.getStudnet(id);
		if(stu==null) {
			System.out.println("没有这个学生");
			return;
		}
		System.out.print("确定删除？\n1.是   2.否");
		int flag = input.nextInt();
		if(flag==1) {
			StudentDAO.delete(id);
			System.out.println("已删除");
			StudentDAO.listAllstudents();
		}
		
	}
	
	public void updateStudent() {
		System.out.println("请输入要修改信息的学生的id：");
		long id = input.nextLong();
		System.out.print("请输入要修改的学生修改后的姓名：");
		String name = input.next();
		StudentDAO.update_name(new Student(id,name));
		StudentDAO.listAllstudents();
	}
	
	public void newTeacher() {
		System.out.println("输入要增加老师的信息：");
		System.out.print("输入老师的工号：");
		long id = input.nextLong();
		System.out.print("输入老师的姓名");
		String name = input.next();
		Teacher teacher = new Teacher(id,name);
		TeacherDAO.newTeacher(teacher);
		System.out.println("增加成功");
		TeacherDAO.listAllTeachers();
	}
	
	public void deleteTeacher() {
		System.out.println("请输入要删除的老师的id：");
		long id = input.nextLong();
		System.out.print("确定删除？\n1.是   2.否");
		int sel = input.nextInt();
		if(sel==1) {
			TeacherDAO.delete(id);
			System.out.println("已删除");
			TeacherDAO.listAllTeachers();
		}
		
	}
	
	public void updateTeacher() {
		System.out.println("请输入要修改信息的老师的id：");
		long id = input.nextLong();
		System.out.print("请输入要修改的老师修改后的姓名：");
		String name = input.next();
		TeacherDAO.update_name(new Teacher(id,name));
		TeacherDAO.listAllTeachers();
	}
	
	public void getStudent() {
		System.out.print("请输入要查询的学生的id：");
		long id = input.nextLong();
		Student stu = StudentDAO.getStudnet(id);
		if(stu!=null) {
			System.out.print("学号"+stu.getId()+"\t");
			System.out.print("姓名"+stu.getName()+"\t");
			if(stu.getTeacher()!=null) {
				System.out.println("指导老师"+stu.getTeacher().getId());
			}
			else {
				System.out.println("没有指导老师");
			}
		}
		else {
			System.out.println("没有这个学生");
		}
		
	}
	
	public void getInstructor() {//获取老师指导的学生
		System.out.print("输入老师的id：");
		long id = input.nextLong();
		StudentDAO.listTeacherINTRstudents(id);
	}
	
	public void selectTutor() {
		System.out.println("请输入学生的id：");
		long id = input.nextLong();
		Student stu = StudentDAO.getStudnet(id);
		if(stu!=null) {
			System.out.print("学号"+stu.getId()+"\t");
			System.out.println("姓名"+stu.getName()+"\t");
			if(stu.getTeacher()!=null) {
				System.out.println("指导老师"+stu.getTeacher().getId());
				return;
			}
			else {
				System.out.println(stu.getTeacher());
			}
		}
		else {
			System.out.println("没有这个学生");
		}
		
		System.out.println("以下是导师的信息：");
		TeacherDAO.listAllTeachers();
		System.out.println("输入要选择的导师的id");
		id = input.nextLong();
		Teacher tea = TeacherDAO.getTeacher(id);
		while(tea==null) {
			System.out.println("没有这个导师，请重新输入id,退出输入0");
			id = input.nextLong();
			if(id==0) return;
			else tea = TeacherDAO.getTeacher(id);
		}
		stu.setTeacher(tea);
		StudentDAO.update_tutor(stu);
		System.out.println("选择成功");
		StudentDAO.listAllstudents();
	}
	
	public void updateTutor() {
		System.out.println("请输入要修改导师的学生的id：");
		long id = input.nextLong();
		Student stu = StudentDAO.getStudnet(id);
		if(stu!=null) {
			System.out.print("学号"+stu.getId()+"\t");
			System.out.println("姓名"+stu.getName()+"\t");
			if(stu.getTeacher()!=null) {
				System.out.println("指导老师"+stu.getTeacher().getId());
			}
			else {
				System.out.println(stu.getTeacher());
			}
		}
		else {
			System.out.println("没有这个学生");
		}
		System.out.println("以下是导师的信息：");
		TeacherDAO.listAllTeachers();
		System.out.println("输入要选择的导师的id");
		id = input.nextLong();
		Teacher tea = TeacherDAO.getTeacher(id);
		while(tea==null) {
			System.out.println("没有这个导师，请重新输入id,退出输入0");
			id = input.nextLong();
			if(id==0) return;
			else tea = TeacherDAO.getTeacher(id);
		}
		stu.setTeacher(tea);
		StudentDAO.update_tutor(stu);
		System.out.println("修改成功");
		StudentDAO.listAllstudents();
	}
	
	public void selectStudent() {
		System.out.println("输入导师的id");
		long id = input.nextLong();
		Teacher tea = TeacherDAO.getTeacher(id);
		if(tea!=null) {
			System.out.print("工号"+tea.getId()+"\t");
			System.out.println("姓名"+tea.getName()+"\t");
		}
		else {
			System.out.println("没有这个老师");
		}
		while(tea==null) {
			System.out.println("没有这个导师，请重新输入id,退出输入0");
			id = input.nextLong();
			if(id==0) return;
			else tea = TeacherDAO.getTeacher(id);
		}
		System.out.println("以下是学生信息：");
		StudentDAO.listAllstudents();
		System.out.println("输入要指导的学生的id");
		long stu_id = input.nextLong();
		Student stu = StudentDAO.getStudnet(stu_id);
		if(stu!=null) {
			System.out.print("学号"+stu.getId()+"\t");
			System.out.println("姓名"+stu.getName()+"\t");
			if(stu.getTeacher()!=null) {
				System.out.println("指导老师"+stu.getTeacher().getId());
				System.out.println("该学生已经有指导老师，不能再选择");
				return;
			}
			else {
				System.out.println(stu.getTeacher());
			}
		}
		else {
			System.out.println("没有这个学生");
			return;
		}
		stu.setTeacher(tea);
		StudentDAO.update_tutor(stu);
		System.out.println("选择成功");
		System.out.print("学号"+stu.getId()+"\t");
		System.out.println("姓名"+stu.getName()+"\t");
		System.out.println("指导老师"+stu.getTeacher().getId());
		
	}
	
	public void updateINTRStudent() {
		System.out.println("输入导师的id");
		long id = input.nextLong();
		Teacher tea = TeacherDAO.getTeacher(id);
		if(tea!=null) {
			System.out.print("工号"+tea.getId()+"\t");
			System.out.println("姓名"+tea.getName()+"\t");
		}
		else {
			System.out.println("没有这个老师");
		}
		while(tea==null) {
			System.out.println("没有这个导师，请重新输入id,退出输入0");
			id = input.nextLong();
			if(id==0) return;
			else tea = TeacherDAO.getTeacher(id);
		}
		System.out.println("以下是该导师指导的学生信息：");
		StudentDAO.listTeacherINTRstudents(id);
		System.out.println("输入要删除指导的学生的id");
		long stu_id = input.nextLong();
		Student stu = StudentDAO.getStudnet(stu_id);
		if(stu!=null) {
			System.out.print("学号"+stu.getId()+"\t");
			System.out.println("姓名"+stu.getName()+"\t");
			if(stu.getTeacher()!=null) {
				System.out.println("指导老师"+stu.getTeacher().getId());
				System.out.println("该学生已经有指导老师，不能再选择");
				return;
			}
			else {
				System.out.println(stu.getTeacher());
			}
		}
		else {
			System.out.println("没有这个学生");
			return;
		}
		stu.setTeacher(null);
		StudentDAO.update_tutor(stu);
		System.out.println("删除成功");
		System.out.print("学号"+stu.getId()+"\t");
		System.out.println("姓名"+stu.getName()+"\t");
		if(stu.getTeacher()==null) {
			System.out.println("指导老师"+stu.getTeacher());
		}
		else
			System.out.println("指导老师"+stu.getTeacher().getId());
	}	
	
	
}